//
//  NewfeatureCollectionViewController.swift
//  Weibo
//
//  Created by .. on 16/5/2.
//  Copyright © 2016年 Lizhuo. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewfeatureCollectionViewController: UICollectionViewController {

    //页面个数
    private let pageCount = 4
    //布局对象
    private var layout: UICollectionViewFlowLayout = NewfeatureLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
 /*       //1.设置layout布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        //2.设置collectionView的属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true */
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //1.注册
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewfeatureCell
        cell.imageIndex = indexPath.row
        return cell
    }
    
    //完全显示一个cell之后调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //1.拿到当前显示的cell对应的索引
        let path = collectionView.indexPathsForVisibleItems().last
        if path?.item == (pageCount - 1) {
            //2.拿到当前索引对应的cell
            let cell = collectionView.cellForItemAtIndexPath(path!) as! NewfeatureCell
            //3.让cell执行动画
            cell.startBtnAnimation()
        }
        
    }

}

class NewfeatureCell: UICollectionViewCell {
    
    private var imageIndex: Int? {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
        }
    }
    
    func startBtnAnimation() {
        if imageIndex == 3 {
            startButton.hidden = false
            
            //执行动画
            startButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
            startButton.userInteractionEnabled = false
            
            //UIViewAnimationOptions(rawValue: 0)不需要枚举值
            UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: {
                //清空变形
                self.startButton.transform = CGAffineTransformIdentity
                }, completion: { (_) in
                    self.startButton.userInteractionEnabled = true
            })
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //1.初始化UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startBtnClick() {
        //跳转主页 发通知
        NSNotificationCenter.defaultCenter().postNotificationName(LZSwitchRootViewController, object: true)
    }
    
    private func setupUI() {
        //1.添加控件到contentView上
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        //2.布局子控件的位置
        iconView.xmg_Fill(contentView)
        startButton.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
    
    //MARK: - 懒加载
    private lazy var iconView = UIImageView()
    private lazy var startButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: #selector(NewfeatureCell.startBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        btn.hidden = true
        return btn
    }()
    
}

private class NewfeatureLayout: UICollectionViewFlowLayout {
    
    //准备布局
    override func prepareLayout() {
        //1.设置layout布局
        itemSize = UIScreen.mainScreen().bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = .Horizontal
        
        //2.设置collectionView的属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
    }
    
}
